﻿const sqlite3 = require('sqlite3').verbose()
const config = require('../config/config')

//let db = require('sqlite3').verbose();
/*
exports.db = db;
exports.setup = function() {

    const database = new sqlite3.Database(config.database.name, function(err){
 	if (err)
	{ 
		throw err;
	}else{
    
        sql.setDialect('sqlite');
		database.run(config.record.create().ifNotExists().toQuery().text, function(err){
            if(err){
                console.log(err);
            }else{
                console.log(config.record.create().ifNotExists().toQuery().text);
            }
        });
        database.close();
	}
});


}
exports.open = function(path) {
    return new Promise(function(resolve) {
    this.db = new sqlite3.Database(config.database.name,
        function(err) {
            if(err) reject("Open error: "+ err.message)
            else  resolve(path + " opened")
        }
    )  
    })
}
 
// any query: insert/delete/update
exports.run = function(query) {
    return new Promise(function(resolve, reject) {
        this.db.run(query,
            function(err)  {
                if(err) reject(err.message)
                else    resolve(true)
        })
    })   
}
// first row read
exports.query = function(query, params) {

    return new Promise(function(resolve, reject) {
        let db =  new sqlite3.Database(config.database.name,
            function(err) {
                if(err) {
                    reject("Open error: "+ err.message)
                    return;
                }
            }
        )  

        db.all(query, params, function(err, row)  {
            console.log(row);
            if(err){

                reject("Read error: " + err.message)
            }
            else {
                db.close(); 
                resolve(row)
            }
        })
                
    })

}
// first row read
exports.get=function(query, params) {
    return new Promise(function(resolve, reject) {

        this.db.get(query, params, function(err, row)  {

            if(err) reject("Read error: " + err.message)
            else {
                resolve(row)
            }
        })
    })
}
 
// set of rows read
exports.all = function(query, params) {
    return new Promise(function(resolve, reject) {
        if(params == undefined) params=[]
 
        this.db.all(query, params, function(err, rows)  {
            if(err) reject("Read error: " + err.message)
            else {
                resolve(rows)
            }
        })
    })
}
 
// each row returned one by one
exports.each = function(query, params, action) {
    return new Promise(function(resolve, reject) {
        var db = this.db
        db.serialize(function() {
            db.each(query, params, function(err, row)  {
                if(err) reject("Read error: " + err.message)
                else {
                    if(row) {
                        action(row)
                    }   
                }
            })
            db.get("", function(err, row)  {
                resolve(true)
            })           
        })
    })
}
 
exports.close=function() {
    return new Promise(function(resolve, reject) {
        this.db.close()
        resolve(true)
    })
}

*/

let query = ( sql, values) => {
  return new Promise(function(resolve, reject) {
        let db =  new sqlite3.Database(config.sqlite.database.name,
            function(err) {
                if(err) {
                    reject("Open error: "+ err.message)
                    return;
                }
            }
        )  

        db.all(sql, values, function(err, row)  {
            console.log(row);
            if(err){

                reject("Read error: " + err.message)
            }
            else {
                db.close(); 
                resolve(row)
            }
        })
                
    })
}
module.exports = query;
//module.exports = exports